package image.core;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class ImageInit {
	private double[][] Y, U, V;
	private Color[][] pic;
	static private int width,height;

	public ImageInit(String picName) {

		File filepath = new File(picName);
		//System.out.println("读取路径为："+filepath.getAbsolutePath());
		if(!filepath.exists()) {
			System.out.println("[INFO] 文件路径不存在!");
			return;
		}
		BufferedImage image = null;
		try {
			image = ImageIO.read(filepath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (image!=null)
		{
			System.out.println("读取图片为 :"+filepath.getAbsolutePath().substring(filepath.getAbsolutePath().lastIndexOf("\\")+1,filepath.getAbsolutePath().length()));
			width = image.getWidth();
			height = image.getHeight();
			int w = width%16==0?width:(1+width/16)*16;
			int h = height%16==0?height:(1+height/16)*16;
			pic = new Color[w][h];
			for (int i = 0; i < width ; i++) {
				for (int j = 0; j < height; j++) {
					pic[i][j] = new Color(image.getRGB(i, j));
				}
			}
			Color color = new Color(0,0,0);
			if (w!=width||h!=height)
			{
				if (w!=width&&h!=height)
				{
					for (int i = 0;i<width;i++)
					{
						for (int j= height;j<h;j++)
						{
							pic[i][j] = color;
						}
					}
					for (int i = width;i<w;i++)
					{
						for (int j= 0;j<h;j++)
						{
							pic[i][j] = color;
						}
					}
				}
				for (int i = w==width?0:width;i<w;i++)
				{
					for (int j = h==height?0:height;j<h;j++)
					{
						pic[i][j] = color;
					}
				}
			}
		}


		System.err.println("please await .....");
	}

	/**
	 * 颜色编码处理器
	 */
	public void RGB2YUV() {
		Y = new double[pic.length][pic[1].length];
		U = new double[pic.length / 2][pic[1].length / 2];
		V = new double[pic.length / 2][pic[1].length / 2];
		for (int i = 0; i < pic.length / 2; i++)
			for (int j = 0; j < pic[1].length / 2; j++) {
				U[i][j] = V[i][j] = 0;
			}
		for (int i = 0; i < pic.length; i++)
			for (int j = 0; j < pic[1].length; j++) {
				if (Objects.isNull(pic[i][j])) {
					continue;
				}
				int r = pic[i][j].getRed();
				int g = pic[i][j].getGreen();
				int b = pic[i][j].getBlue();
			/*	//TODO 计算公式 活性
				Y[i][j] = 0.299 * r + 0.587 * g + 0.114 * b;
				U[i / 2][j / 2] = -0.169 * r - 0.331 * g + 0.5 * b + 128;
				V[i / 2][j / 2] = 0.5 * r - 0.419 * g - 0.081 * b + 128;*/
				Y[i][j] = 0.299 * r + 0.587 * g + 0.114 * b;
				U[i / 2][j / 2] = -0.169 * r - 0.331 * g + 0.5 * b + 128;
				V[i / 2][j / 2] = 0.5 * r - 0.419 * g - 0.081 * b + 128;
			}
		PreCoding.write2File(ImageCoding.Y_PATH+"Y_256x256_encoding.txt", Y);
		PreCoding.write2File(ImageCoding.U_PATH+"U_128x128_encoding.txt", U);
		PreCoding.write2File(ImageCoding.V_PATH+"V_128x128_encoding.txt", V);
	}

	public Color[][] YUV2RGB(double[][] Y, double[][] U, double[][] V) {
		Color[][] pic = new Color[Y.length][Y[0].length];
		for (int i = 0; i < pic.length; i++) {
			for (int j = 0; j < pic[0].length; j++) {
				double y = Y[i][j];
				double u = U[i / 2][j / 2];
				double v = V[i / 2][j / 2];

				int r, g, b;
				r = (int) (y + 1.13983 * (v - 128));
				g = (int) (y - 0.39465 * (u - 128) - 0.58060 * (v - 128));
				b = (int) (y + 2.03211 * (u - 128));
				r = r > 255 ? 255 : r < 0 ? 0 : r;
				g = g > 255 ? 255 : g < 0 ? 0 : g;
				b = b > 255 ? 255 : b < 0 ? 0 : b;
				try {
					pic[i][j] = new Color(r, g, b);
				}catch (Exception e){
					e.printStackTrace();
				}

			}
		}
		return pic;
	}

	public  double[][] getY() {
		return Y.clone();
	}

	public double[][] getU() {
		return U.clone();
	}

	public double[][] getV() {
		return V.clone();
	}

	public static void writeImage(Color[][] pic, String name, String type) {
		File writerFilePath = null;
		try {
			BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			for (int i = 0; i < width; i++)
				for (int j = 0; j < height; j++) {
					bi.setRGB(i, j, pic[i][j].getRGB());
				}
			// MIME 类型重新定义
			Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(type);
			ImageWriter writer = it.next();
			ImageOutputStream ios;
			 writerFilePath = new File(name + "." + type);
			ios = ImageIO.createImageOutputStream(writerFilePath);
			writer.setOutput(ios);
			writer.write(bi);
			bi.flush();
			ios.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.err.println("写出到 -》》"+writerFilePath.getAbsolutePath()+" success ~");
	}


}
